package cn.mw.cmdb.param;

import cn.mw.cmdb.entity.ModelInfo;
import cn.mw.cmdb.entity.ModelType;
import cn.mw.cmdb.entity.MwCMDBConfigInfo;
import cn.mw.cmdb.service.ServiceChoose;
import cn.mw.cmdb.service.impl.CmdbServiceManage;
import cn.mw.cmdb.service.impl.CustomModelServiceImpl;
import cn.mw.cmdb.service.impl.MwCMDBConfigImpl;
import cn.mw.cmdb.util.CMDBTool;
import cn.mw.components.mongodb.entity.CriteriaOpsType;
import cn.mw.components.mongodb.entity.SearchParam;
import cn.mw.components.mongodb.entity.SubCondition;
import cn.mw.microMonitorCommon.util.SpringUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

import static cn.mw.cmdb.enums.ModelConfigKeyEnum.systemDevType;
import static cn.mw.cmdb.param.SystemDevTypeEnum.scBankSystemDev;

@Data
public class ModelSearchParam extends SearchParam implements ServiceChoose, SearchParamConvert {
    @Schema(description = "分组id")
    private String groupId;

    private String modelId;

    private List<String> modelIds;

    private String modelType;

    private String modelViewType;

    @Override
    public String getServiceId() {
        return CustomModelServiceImpl.ID;
    }

    @Override
    public void convert() throws Exception {
        //如果是四川银行，模型分组，模型查询时，不做权限过滤
        CmdbServiceManage cmdbServiceManage = SpringUtils.getBean(CmdbServiceManage.class);
        MwCMDBConfigImpl configServer = (MwCMDBConfigImpl) cmdbServiceManage.getModelService(MwCMDBConfigImpl.ID);
        MwCMDBConfigInfo cmdbConfigInfo = configServer.doSelectByKey(systemDevType.name());
        Object configValue = cmdbConfigInfo.getConfigValue();
        if (configValue instanceof String) {
            String  systemType = (String) configValue;
            if(scBankSystemDev.name().equals(systemType)){
                this.setIgnoreDataAuth(true);
            }
        }


        if (CollectionUtils.isNotEmpty(modelIds)) {
            this.addSubCondition(CMDBTool.getFieldName(ModelInfo::getId)
                    , new SubCondition(CriteriaOpsType.in.name(), modelIds));
        }
        if (StringUtils.hasText(modelId)) {
            this.addSubCondition(CMDBTool.getFieldName(ModelInfo::getId)
                    , new SubCondition(CriteriaOpsType.is.name(), modelId));
        }
        if (StringUtils.hasText(groupId)) {
            this.addSubCondition(CMDBTool.getFieldName(ModelInfo::getGroupPath)
                    , new SubCondition(CriteriaOpsType.in.name(), groupId));
        }
        if (StringUtils.hasText(modelType)) {
            this.addSubCondition(CMDBTool.getFieldName(ModelInfo::getModelType)
                    , new SubCondition(CriteriaOpsType.is.name(), ModelType.Normal.getType()));
        }
        if (StringUtils.hasText(modelViewType)) {
            this.addSubCondition(CMDBTool.getFieldName(ModelInfo::getModelViewType)
                    , new SubCondition(CriteriaOpsType.is.name(), modelViewType));
        }
    }
}
